﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Client
{
    public partial class MainForm : Form
    {
        #region -  VARIABLES  -
        private SendFile sendFile = null;
        private ClientProgram client = null;
        private DataPackage package = null;
        private Login login = null;
        private string ipAddress = "";
        #endregion

        #region -  THREADS  -
        private Thread tShowMessageBox = null;
        private Thread tShowProgessBar = null;
        private Thread tShowAttendenceCheck = null;
        #endregion

        #region -  CONTRUCTOR  -
        public MainForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            this.ClientInstance("192.168.50.131");

            SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
        }

        void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
        {
            if (e.Reason == SessionEndReasons.Logoff || e.Reason == SessionEndReasons.SystemShutdown)
            {
                this.client.StopClient();
            }
        }

        void login_OnSetServerIPAddress(object sender, SetServerIPAddressEventArgs e)
        {
            this.ipAddress = e.IpAddress;
            this.ClientInstance(this.ipAddress);
            this.login.Close();
        }        
        #endregion

        private void ClientInstance(string ipAddress)
        {
            this.client = new ClientProgram(ipAddress);
            this.client.OnReceiveMessage += new ReceiveMessageEventHandler(client_OnReceiveMessage);
            this.client.OnReceiveFilename += new ReceiveFilenameEventHandler(client_OnReceiveFilename);
            this.client.OnReceiveFilePercent += new ReceiveFilePercentEventHandler(client_OnReceiveFilePercent);
            this.client.OnSendFilename += new SendFilenameEventHandler(client_OnSendFilename);
            this.client.OnSendFilePercent += new SendFilePercentEventHandler(client_OnSendFilePercent);
            this.client.OnActiveAttendenceCheck += new ActiveAttendenceCheckEventHandler(client_OnActiveAttendenceCheck);
            this.client.OnRestartClient += new RestartClientEventHandler(client_OnRestartClient);
        }

        void client_OnRestartClient(object sender, EventArgs e)
        {
            this.client.StopClient();
            this.ClientInstance(this.ipAddress);
        }

        #region -  ATTENDENCE CHECK  -
        void client_OnActiveAttendenceCheck(object sender, ActiveAttendenceCheckEventArgs e)
        {
            ClientProgram c = (ClientProgram)sender;
            c.SetAttendenceCheck();
            this.tShowAttendenceCheck = new Thread(new ParameterizedThreadStart(ShowAttendenceCheck));
            this.tShowAttendenceCheck.Start(c);
        }

        private void ShowAttendenceCheck(object sender)
        {
            ClientProgram c = (ClientProgram)sender;
            c.ShowAttendenceCheck();
            this.tShowAttendenceCheck.Abort();
        }
        #endregion

        #region -  SEND FILE  -
        private void client_OnSendFilePercent(object sender, PercentEventArgs e)
        {
            ClientProgram c = (ClientProgram)sender;
            c.SetProgressBarValue(e.Percent);
            c.SetProgressBarTitle("Đang gửi ... " + e.Percent + " %");
        }

        private void client_OnSendFilename(object sender, FilenameEventArgs e)
        {
            try
            {
                ClientProgram c = (ClientProgram)sender;
                c.SetProgressBar();
                c.SetProgressBarMessage("Đang gửi tệp tin '" + e.Filename + "' đến máy chủ");
                this.tShowProgessBar = new Thread(new ParameterizedThreadStart(ShowProgressBar));
                this.tShowProgessBar.Start(c);
            }
            catch
            { }
        }
        #endregion

        #region -  RECEIVE FILE  -
        private void client_OnReceiveFilePercent(object sender, PercentEventArgs e)
        {
            ClientProgram c = (ClientProgram)sender;
            c.SetProgressBarValue(e.Percent);
            c.SetProgressBarTitle("Đang nhận ... " + e.Percent + " %");
        }

        private void client_OnReceiveFilename(object sender, FilenameEventArgs e)
        {
            try
            {
                ClientProgram c = (ClientProgram)sender;
                c.SetProgressBar();
                c.SetProgressBarMessage("Đang nhận '" + e.Filename + "' từ máy chủ.");
                this.tShowProgessBar = new Thread(new ParameterizedThreadStart(ShowProgressBar));
                this.tShowProgessBar.Start(c);
            }
            catch
            { }
        }

        private void ShowProgressBar(object obj)
        {
            ClientProgram c = (ClientProgram)obj;
            c.ShowProgressBar();
            this.tShowProgessBar.Abort();
        }
        #endregion

        #region -  RECEIVE MESSAGE  -
        private void client_OnReceiveMessage(object sender, MessageEventArgs e)
        {
            ClientProgram c = (ClientProgram)sender;
            c.SetMessageBox();
            c.SetMessage(e.Message);
            this.tShowMessageBox = new Thread(new ParameterizedThreadStart(this.ShowMessageBox));
            this.tShowMessageBox.Start(c);
        }

        private void ShowMessageBox(object sender)
        {
            ClientProgram c = (ClientProgram)sender;
            c.ShowMessageBox();
            this.tShowMessageBox.Abort();
        }
        #endregion

        #region -  SEND MESSAGE EVENT  -
        private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.client.SetMessageBox();
            this.client.ShowMessageBox1();
        }
        #endregion

        #region -  SEND MESSAGE  -
        private void sendMessage_OnSendMessage(object sender, MessageEventArgs e)
        {
            this.package = new DataPackage(Command.Text, null, e.Message.Length);
            this.client.sendQueue.Enqueue(this.package);
            this.client.sendQueue.Enqueue(e.Message);
        }
        #endregion

        #region -  FORM CLOSED  -
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.client.StopClient();
        }
        #endregion

        #region -  SEND FILE  -
        private void sendFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.sendFile = new SendFile();
            this.sendFile.OnGetFilePath += new GetFilePathEventHandler(sendFile_OnGetFilePath);
            this.sendFile.Show();
        }
        #endregion

        #region -  GET FILE PATH BEFORE SEND FILE  -
        private void sendFile_OnGetFilePath(object sender, FileInfoEventArgs e)
        {
            this.package = new DataPackage(Command.Data, Path.GetFileName(e.FilePath), e.Length);
            this.client.sendQueue.Enqueue(this.package);
            this.client.SetFilePath(e.FilePath);
        }
        #endregion

        #region -  FORM LOAD  -
        private void MainForm_Load(object sender, EventArgs e)
        {
            TcpChannel chan = new TcpChannel(6600);
            ChannelServices.RegisterChannel(chan,false);
            RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("ScreenCapture, ScreenCapture"), "MyCaptureScreenServer", WellKnownObjectMode.Singleton);
            this.Size = new Size(1, 1);
        }
        #endregion

        private bool TestProcess()
        {
            try
            {
                foreach (Process p in Process.GetProcesses())
                {
                    if (p.ProcessName.Equals("Client"))
                        return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        #region -  EXIT  -
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void setServerIPAddressToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.login = new Login();
            this.login.OnSetServerIPAddress += new SetServerIPAddressEventHandler(login_OnSetServerIPAddress);
            this.login.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }
    }
}
